package com.zlx.store.coupon.config;

import com.zlx.store.coupon.job.BfCouponJob;
import com.zlx.store.coupon.job.CouponJob;
import org.quartz.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.Random;

@Configuration
public class CouponJobConfig {
    /**
     * 配置要启动的任务
     * @return
     */
    @Bean
    public JobDetail bfCouponJobDetail(){
        return JobBuilder.newJob(BfCouponJob.class).storeDurably(true).build();
    }

    @Bean
    public JobDetail couponJobDetail() {
        return JobBuilder.newJob(CouponJob.class).storeDurably(true).build();
    }

    /**
     * 什么时间触发定时器
     * @return
     */
    @Bean
    public Trigger bfCouponJobTrigger(){
        String cron = "0 10 3 * * ?";
        return TriggerBuilder.newTrigger()
                .forJob(bfCouponJobDetail())
                .withSchedule(CronScheduleBuilder.cronSchedule(cron))
                .build();
    }

    /**
     * 定时同步redis和数据库以及自动预热
     * @return
     */
    @Bean
    public Trigger CouponJobTrigger(){
        String cron = "0 31 3 * * ?";
        return TriggerBuilder.newTrigger()
                .forJob(couponJobDetail())
                .withSchedule(CronScheduleBuilder.cronSchedule(cron))
                .build();
    }
}
